import java.util.LinkedList;
import java.util.Queue;

/**
 * A class that simulates a stack using two queues.
 */
public class MyStack {
    private Queue<Integer> mainQueue;
    private Queue<Integer> helperQueue;

    /**
     * Initialize your data structure here.
     */
    public MyStack() {
        mainQueue = new LinkedList<>();
        helperQueue = new LinkedList<>();
    }

    /**
     * Push element x onto stack.
     *
     * @param x the element to be pushed onto the stack
     */
    public void push(int x) {
        helperQueue.offer(x);
        while (!mainQueue.isEmpty()) {
            helperQueue.offer(mainQueue.poll());
        }
        Queue<Integer> temp = mainQueue;
        mainQueue = helperQueue;
        helperQueue = temp;
    }

    /**
     * Removes the element on top of the stack and returns that element.
     *
     * @return the element on top of the stack
     */
    public int pop() {
        if (mainQueue.isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }
        return mainQueue.poll();
    }

    /**
     * Get the top element.
     *
     * @return the top element of the stack
     */
    public int top() {
        if (mainQueue.isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }
        return mainQueue.peek();
    }

    /**
     * Returns whether the stack is empty.
     *
     * @return true if the stack is empty, false otherwise
     */
    public boolean empty() {
        return mainQueue.isEmpty();
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);

        System.out.println("Top element: " + stack.top()); // Should print 3
        System.out.println("Pop element: " + stack.pop()); // Should print 3
        System.out.println("Is stack empty? " + stack.empty()); // Should print false
        System.out.println("Pop element: " + stack.pop()); // Should print 2
        System.out.println("Pop element: " + stack.pop()); // Should print 1
        System.out.println("Is stack empty? " + stack.empty()); // Should print true
    }
}